#include "RedSoldierBulletBehavior.h"




void RedSoldierBulletBehavior::setLeftDirection()
{
	_data->getRect()->Vx = -_RED_SOILDER_BULLET_1_VX;
}

void RedSoldierBulletBehavior::setRightDirection()
{
	_data->getRect()->Vx = _RED_SOILDER_BULLET_1_VX;
}

void RedSoldierBulletBehavior::setTargetDirection()
{
	// init data for funtion
	double targetX = _data->target->X;
	double targetY = _data->target->Y;

	double bulletX = _data->getRect()->X;
	double bulletY = _data->getRect()->Y;

	int Vy = 0;

	double k  = (targetY - bulletY) / abs(bulletX - targetX);
	double absK = abs(k);

	if (absK < 1.5) {
		// Check left or right 
		if (targetX < bulletX ) {
			this->setLeftDirection();
		}
		else {
			this->setRightDirection();
		}

		Vy =_RED_SOILDER_BULLET_1_VY * k;
		Vy = Vy % 2 == 0 ? Vy : Vy + 1;		
	} else {
		Vy =_RED_SOILDER_BULLET_1_VY * (k / absK);
	}

	if (bulletX > targetX) {
		bulletX -= 17;
	} else {
		bulletX += 17;
	}

	if (Vy > 0) {
		bulletY += 40;
	} else if (Vy < 0) {
		bulletY -= 10;
	} else {
		bulletY += 18;
	}

	_data->getRect()->setPosition(bulletX, bulletY);
	_data->getRect()->Vy = Vy;
}

